package com.base.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.sqlparam.RegisterinfoesExamplePage;
import com.base.po.voip.Registerinfoes;
import com.base.po.voip.RegisterinfoesExample.Criteria;
import com.base.service.LogsService;
import com.base.service.RegisterinfoesService;
import com.base.utils.DateUtil;
import com.base.utils.IP;
import com.base.utils.Page;
import com.base.utils.Produce;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_zhucebaobiao {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private RegisterinfoesService registerinfoesService;

	// 注册报表
	@RequestMapping("/content_zhucebaobiaoOld")
	public ModelAndView indexOld(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		RegisterinfoesExamplePage examplePage = new RegisterinfoesExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("RegisterTime");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		List<Registerinfoes> registerinfoes = null;
		Criteria criteria = examplePage.createCriteria();
		switch (adminUsergrade) {
		case 0:

			break;

		default:
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		}/*
			 * 设置分页
			 */
		int totlenum = registerinfoesService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", 1);// 传入session中
		request.setAttribute("date", "01/01/2017 -  ");
		registerinfoes = registerinfoesService.selectByExamplePage(examplePage);
		if (registerinfoes != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_zhucebaobiao");
			request.setAttribute("list", registerinfoes);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "注册报表信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	@RequestMapping("/selectRegisterinfoesOld")
	public ModelAndView selectRegisterinfoesOld(HttpServletRequest request, String mobile, Integer curPage, String date)
			throws Exception {
		// 设置session缓存
		if (date == null) {
			if (request.getSession().getAttribute("date") != null) {
				date = (String) request.getSession().getAttribute("date");
			} else {
				date = "01/01/2017 -  ";
			}
		}
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (mobile == null) {
			if (request.getSession().getAttribute("mobile") != null) {
				mobile = (String) request.getSession().getAttribute("mobile");
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		RegisterinfoesExamplePage examplePage = new RegisterinfoesExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("RegisterTime");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		List<Registerinfoes> registerinfoes = null;
		Criteria criteria = examplePage.createCriteria();
		// 条件筛选
		if (mobile != null && !mobile.equals("")) {
			request.setAttribute("mobile", mobile);
			criteria = criteria.andMobileLike("%" + mobile + "%");
			examplePage.getOredCriteria().add(criteria);
		}
		// 设置时间
		String[] sourceStrArray = date.trim().split("-");
		Date dateFrom = null;
		Date dateTo = null;
		for (int i = 0; i < sourceStrArray.length; i++) {
			System.out.println(sourceStrArray[i]);
			if (dateFrom == null) {
				dateFrom = DateUtil.strToDateLong(sourceStrArray[i]);
			} else {
				dateTo = DateUtil.strToDateLong(sourceStrArray[i]);
			}
		}
		if (dateFrom != null) {
			request.setAttribute("date", date);
			if (dateTo != null) {
				criteria = criteria.andRegistertimeBetween(dateFrom, dateTo);
			} else {
				criteria = criteria.andRegistertimeBetween(dateFrom, Produce.getNowDate());
			}
			examplePage.getOredCriteria().add(criteria);
		}
		switch (adminUsergrade) {
		case 0:

			break;

		default:
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		}/*
			 * 设置分页
			 */
		int totlenum = registerinfoesService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", curPage);// 当前页面传入session中
		registerinfoes = registerinfoesService.selectByExamplePage(examplePage);
		if (registerinfoes != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_zhucebaobiao");
			request.setAttribute("list", registerinfoes);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	@RequestMapping("/content_zhucebaobiao")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		List<Registerinfoes> registerinfoes = null;
		
		switch (adminUsergrade) {
		case 0:
			page.setCurrentPageNo(1);
			page.setRecordTotal(registerinfoesService.countByParentid(null, null, null, null));
			registerinfoes = registerinfoesService.selectByParentid(page, null, null, null, null);
			break;
		default:
			page.setCurrentPageNo(1);
			page.setRecordTotal(registerinfoesService.countByParentid(adminUserId, null, null, null));
			registerinfoes = registerinfoesService.selectByParentid(page, adminUserId, null, null, null);
			break;

		}
		    /*
			 * 设置分页
			 */
		int totlenum = page.getRecordTotal();// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
		request.setAttribute("date", "01/01/2018 -  ");
		if (registerinfoes != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_zhucebaobiao");
			request.setAttribute("list", registerinfoes);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "注册报表信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	@RequestMapping("/selectRegisterinfoes")
	public ModelAndView selectRegisterinfoes(HttpServletRequest request, String mobile, Integer curPage, String date)
			throws Exception {
		// 设置session缓存
		if (date == null) {
			if (request.getSession().getAttribute("date") != null) {
				date = (String) request.getSession().getAttribute("date");
			} else {
				date = "01/01/2018 -  ";
			}
		}
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (mobile == null) {
			if (request.getSession().getAttribute("mobile") != null) {
				mobile = (String) request.getSession().getAttribute("mobile");
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		List<Registerinfoes> registerinfoes = null;
		
		String fMobile = null;
		String fDateFrom = null;
		String fDateTo = null;
		// 条件筛选
		if (mobile != null && !mobile.equals("")) {
			fMobile = mobile;
		}
		// 设置时间
		String[] sourceStrArray = date.trim().split("-");
		Date dateFrom = null;
		Date dateTo = null;
		for (int i = 0; i < sourceStrArray.length; i++) {
			System.out.println(sourceStrArray[i]);
			if (dateFrom == null) {
				dateFrom = DateUtil.strToDateLong(sourceStrArray[i]);
			} else {
				dateTo = DateUtil.strToDateLong(sourceStrArray[i]);
			}
		}
		if (dateFrom != null) {
			fDateFrom = new SimpleDateFormat("yyyy-MM-dd").format(dateFrom) +" 00:00:00";
		}
		if (dateTo != null) {
			fDateTo = new SimpleDateFormat("yyyy-MM-dd").format(dateTo) +" 23:59:59";
		}
		switch (adminUsergrade) {
		case 0:
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(registerinfoesService.countByParentid(null, fMobile, fDateFrom, fDateTo));
			registerinfoes = registerinfoesService.selectByParentid(page, null, fMobile, fDateFrom, fDateTo);
			break;
		default:
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(registerinfoesService.countByParentid(adminUserId, fMobile, fDateFrom, fDateTo));
			registerinfoes = registerinfoesService.selectByParentid(page, adminUserId, fMobile, fDateFrom, fDateTo);
			break;

		}
		    /*
			 * 设置分页
			 */
		int totlenum = page.getRecordTotal();// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		
		if (registerinfoes != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_zhucebaobiao");
			request.setAttribute("mobile", mobile);
			request.setAttribute("date", date);
			request.setAttribute("list", registerinfoes);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}
